package com.youths.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.youths.dao.*;
import com.youths.domain.*;
import com.youths.service.TaskService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Stack;

/**
 * Created by IntelliJ IDEA.
 * User: KingRainGrey
 * Date: 2020/7/14
 */
@Service
public class TaskServiceImp implements TaskService {
    @Autowired(required = false)
    TaskMapper taskMapper;

    @Autowired(required = false)
    YouthInfoMapper youthInfoMapper;

    @Autowired(required = false)
    ExpertInfoMapper expertInfoMapper;

    @Autowired(required = false)
    SliceMapper sliceMapper;

    @Autowired(required = false)
    LabelConsumerMapper labelConsumerMapper;

    @Override
    public int addTask(Task task) {
        return taskMapper.add(task);
    }

    @Override
    public int querySliceId(Integer taskId) {
        return taskMapper.querySliceId(taskId);
    }

    @Override
    public int updateTaskState(Integer expertId, Integer state, Integer taskId) {
        return taskMapper.update(expertId, state, taskId);
    }

    @Override
    public int queryTaskId(Integer slicingId) {
        return taskMapper.queryTaskId(slicingId);
    }

    @Override
    public int updateState(Integer state, Integer taskId) {
        return taskMapper.updateState(state, taskId);
    }



    @Override
    public List<Task> getByState(Integer state) {
        return taskMapper.list(state);
    }

    @Override
    public List<Task> queryByExpert(Integer expertId) {
        return taskMapper.query(expertId);
    }

    @Override
    public List<Task> getAll(Task task) {
        return taskMapper.getAll(task);
    }

    @Override
    public Integer getTotal(Integer state, Integer expertId) {
        return taskMapper.getTotal(state, expertId);
    }

    @Override
    public List<Task> getRecords(Task task) {
        return taskMapper.getRecords(task);
    }

    @Override
    public Integer getRecordNum(Integer expertId) {
        return taskMapper.getRecordNum(expertId);
    }

    @Override
    public PageInfo getTaskList(Integer pageNum,Integer pageSize, Integer slicingId, Integer expertId, String card, String name, Integer state, String order) {
        PageHelper.startPage(pageNum,pageSize,order);
        List<YouthInfo> youthInfos = null;
        List<ExpertInfo> expertInfos = null;
        List<Slice> slices = null;
        if(card!=null && !card.equals("")){
            youthInfos = youthInfoMapper.getList(null,null,card,null);
            if(youthInfos.size()>0){
                slices = sliceMapper.getList(null,null,null,youthInfos);
            }
        }
        if(name!=null && !name.equals("")){
            expertInfos = expertInfoMapper.getList(null,null,name,null,null);
        }
        List<Task> tasks = taskMapper.getList(slicingId,expertId,slices,expertInfos,state);
        PageInfo<Task> taskPageInfo = new PageInfo<>(tasks);
        tasks = taskPageInfo.getList();
        List<JSONObject> jsonObjects = new ArrayList<>();
        for(int i=0;i<tasks.size();i++){
            JSONObject jsonObject = new JSONObject();
            slices = sliceMapper.getList(tasks.get(i).getSlicingId(),null,null,null);
            if(slices.size()>0){
                jsonObject.put("slice",slices.get(0));
                youthInfos = youthInfoMapper.getList(slices.get(0).getYouthId(),null,null,null);
                if(youthInfos.size()>0){
                    jsonObject.put("youth",youthInfos.get(0));
                }else{
                    continue;
                }
            }else {
                continue;
            }
            if(tasks.get(i).getExpertId()!=null && tasks.get(i).getExpertId()!=0){
                expertInfos = expertInfoMapper.getListByConsumerId(tasks.get(i).getExpertId());
                if(expertInfos.size()>0){
                    jsonObject.put("expert",expertInfos.get(0));
                }else {
                    continue;
                }
            }
            jsonObject.put("task",tasks.get(i));
            jsonObjects.add(jsonObject);
        }
        PageInfo<JSONObject> pageInfo = new PageInfo<>();
        BeanUtils.copyProperties(taskPageInfo,pageInfo);
        pageInfo.setList(jsonObjects);
        return pageInfo;
    }

    @Override
    @Transactional
    public JSONObject taskDistribution(Integer expertId, String taskId) {
        String temp[] = taskId.split("/");
        List<String> task = Arrays.asList(temp);
        int result = taskMapper.updateTask(expertId,task);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("total",task.size());
        jsonObject.put("success",result);
        return jsonObject;
    }

    @Override
    public JSONObject cancelTask(String taskId) {
        String temp[] = taskId.split("/");
        List<String> task = Arrays.asList(temp);
        int result = taskMapper.cancelTask(task);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("total",task.size());
        jsonObject.put("success",result);
        return jsonObject;
    }
}
